package aes.gui;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import aes.general.TeacherControl;

/**
 * The Class TeacherStatPanel.
 */
public class TeacherStatPanel extends JPanel {

	/**
	 * This panel is showing the statistics of a specified 
	 * exam chosen by the teacher.
	 */
	private static final long serialVersionUID = 1L;
	private JPanel jp;
	private JLabel title;
	private JButton back;
	private JLabel l2;
	private JComboBox cb;
	private JButton b2;
	private JLabel lblAtendence;
	private JLabel lblAv;
	private JLabel lblExamDate;
	private JLabel lblTotal;
	private JLabel lblM;
	private JLabel lblDistribution;
	private JTextField datetext;
	private JTextField numtext;
	private JTextField t3;
	private JTextField avgtext;
	private static JTextField midtext;
	private JSeparator separator;
	private ArrayList<Double> stats;
	private static DefaultTableModel model;
	private ArrayList<Integer> Grades ;
	private static JTable table;
	private ArrayList<String> ExamId;
	private ArrayList<String> Examinfo;
	
	/**
	 * Create the panel.
	 * 
	 * @param contentPane
	 *            the content pane
	 */
	public TeacherStatPanel(JPanel contentPane) {
		this.jp = contentPane;
		setLayout(null);
		initialize();
	}
		
	/**
	 * Initialize.
	 */
	private void initialize() {
		separator = new JSeparator();
		separator.setBounds(10, 43, 620, 2);
		add(separator);
		
		title = new JLabel("Exam Statistics");
		title.setFont(new Font("Tempus Sans ITC", Font.BOLD, 21));
		title.setBounds(234, 11, 172, 29);
		add(title);
		
		back = new JButton("Cancel");
		back.setBounds(274, 389, 91, 23);
		add(back);
		back.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CardLayout cl = (CardLayout) (jp.getLayout());
				cl.show(jp, "mainPanel");	
				
			}
		});
		
		l2 = new JLabel("Choose Exam To See:");
		l2.setBounds(27, 78, 149, 14);
		add(l2);
		
		cb = new JComboBox();
		cb.setBounds(186, 75, 85, 20);
		add(cb);
		cb.addItem("-------");
		
		while((ExamId=TeacherControl.getStatExam())==null);
		for(String id:ExamId)
			cb.addItem(id);
	
		cb.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				if(cb.getSelectedIndex() == 0)
				{
					avgtext.setText(null);
					midtext.setText(null);
					numtext.setText(null);
					model.insertRow(1,new Object[]{null,null,null,null,null,null,null,null,null,null});
					model.removeRow(2);
					datetext.setText(null);
					t3.setText(null);
				}
				else{
					TeacherControl.RequestGrades((String)cb.getSelectedItem());
					TeacherControl.getExamInfo((String)cb.getSelectedItem());	
					while((Grades =TeacherControl.getGrades())==null);
					while((Examinfo =TeacherControl.getExaminfo())==null);
					stats = TeacherControl.CulcStudentStats(Grades); 
					avgtext.setText(stats.get(0).toString());
					midtext.setText(stats.get(1).toString());
					numtext.setText(stats.get(2).toString());
					datetext.setText(Examinfo.get(0));
					t3.setText(Examinfo.get(1));
					int[] dis = TeacherControl.getDistribution(Grades);
					model.insertRow(1,new Object[]{dis[0],dis[1],dis[2],dis[3],dis[4],dis[5],dis[6],dis[7],dis[8],dis[9]});
					model.removeRow(2);
					TeacherControl.setGrades(null);
					TeacherControl.setExaminfo(null);
				}
			}
		});
		
		
		lblAtendence = new JLabel("Students Attended:");
		lblAtendence.setBounds(27, 187, 119, 14);
		add(lblAtendence);
		
		lblAv = new JLabel("Average:");
		lblAv.setBounds(353, 148, 69, 14);
		add(lblAv);
		
		lblExamDate = new JLabel("Exam Date:");
		lblExamDate.setBounds(27, 145, 79, 14);
		add(lblExamDate);
		
		lblTotal = new JLabel("Exam Duration:");
		lblTotal.setBounds(27, 224, 92, 14);
		add(lblTotal);
		
		lblM = new JLabel("Median:");
		lblM.setBounds(353, 187, 69, 14);
		add(lblM);
		
		lblDistribution = new JLabel("Grade Distribution:");
		lblDistribution.setBounds(259, 285, 121, 14);
		add(lblDistribution);
		
		datetext = new JTextField();
		datetext.setFont(new Font("Tahoma", Font.BOLD, 14));
		datetext.setEditable(false);
		datetext.setBounds(140, 142, 103, 20);
		add(datetext);
		datetext.setColumns(10);
		
		numtext = new JTextField();
		numtext.setFont(new Font("Tahoma", Font.BOLD, 14));
		numtext.setEditable(false);
		numtext.setColumns(10);
		numtext.setBounds(140, 182, 103, 20);
		add(numtext);
		
		t3 = new JTextField();
		t3.setFont(new Font("Tahoma", Font.BOLD, 14));
		t3.setEditable(false);
		t3.setColumns(10);
		t3.setBounds(140, 219, 103, 20);
		add(t3);
		
		avgtext = new JTextField();
		avgtext.setFont(new Font("Tahoma", Font.BOLD, 14));
		avgtext.setEditable(false);
		avgtext.setColumns(10);
		avgtext.setBounds(455, 143, 103, 20);
		add(avgtext);
		
		midtext = new JTextField();
		midtext.setFont(new Font("Tahoma", Font.BOLD, 14));
		midtext.setEditable(false);
		midtext.setColumns(10);
		midtext.setBounds(455, 182, 103, 20);
		add(midtext);
		
		String col[] = {"1","2","3","4","5","6","7","8","9","10"};
		String data[][] = {{}};
		model = new DefaultTableModel(data,col);
		table = new JTable(model);
		table.setBackground(new Color(240, 255, 255));
		table.setFont(new Font("Tahoma", Font.BOLD, 12));
		table.setBounds(55, 310, 529, 39);
		add(table);
		int j = 0;
		model.insertRow(j,new Object[]{"0-9","10-19","20-29","30-39","40-49","50-59","60-69","70-79"
				,"80-89","90-100"});

	}
	
}
